{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Efficient-CapsNet Model Train\n",
    "\n",
    "In this notebook we provide a simple interface to train Efficient-CapsNet on the three dataset discussed in \"Efficient-CapsNet: Capsule Network with Self-Attention Routing\":\n",
    "\n",
    "- MNIST (MNIST)\n",
    "- smallNORB (SMALLNORB)\n",
    "- Multi-MNIST (MULTIMNIST)\n",
    "\n",
    "The hyperparameters have been only slightly investigated. So, there's a lot of room for improvements. Good luck!\n",
    "\n",
    "**NB**: remember to modify the \"config.json\" file with the appropriate parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:38.152068Z",
     "start_time": "2021-01-28T14:17:38.145241Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:39.436665Z",
     "start_time": "2021-01-28T14:17:38.152986Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from utils import Dataset, plotImages, plotWrongImages, plotHistory\n",
    "from models import EfficientCapsNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:39.485046Z",
     "start_time": "2021-01-28T14:17:39.438120Z"
    }
   },
   "outputs": [],
   "source": [
    "gpus = tf.config.experimental.list_physical_devices('GPU')\n",
    "tf.config.experimental.set_visible_devices(gpus[0], 'GPU')\n",
    "tf.config.experimental.set_memory_growth(gpus[0], True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:39.502857Z",
     "start_time": "2021-01-28T14:17:39.486169Z"
    }
   },
   "outputs": [],
   "source": [
    "# some parameters\n",
    "model_name = 'MNIST'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.0 Import the Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:39.898397Z",
     "start_time": "2021-01-28T14:17:39.503821Z"
    }
   },
   "outputs": [],
   "source": [
    "dataset = Dataset(model_name, config_path='config.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 Visualize imported dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:17:41.229443Z",
     "start_time": "2021-01-28T14:17:39.899261Z"
    }
   },
   "outputs": [],
   "source": [
    "n_images = 20 # number of images to be plotted\n",
    "plotImages(dataset.X_test[:n_images,...,0], dataset.y_test[:n_images], n_images, dataset.class_names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.0 Load the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:21:46.634945Z",
     "start_time": "2021-01-28T14:21:46.311296Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "model_train = EfficientCapsNet(model_name, mode='train', verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-25T17:38:06.189031Z",
     "start_time": "2021-01-25T17:38:05.460415Z"
    }
   },
   "source": [
    "# 3.0 Train the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:22:02.087316Z",
     "start_time": "2021-01-28T14:22:02.031863Z"
    }
   },
   "outputs": [],
   "source": [
    "dataset_train, dataset_val = dataset.get_tf_data() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-28T14:25:09.510250Z",
     "start_time": "2021-01-28T14:24:56.018640Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "history = model_train.train(dataset, initial_epoch=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plotHistory(history)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
